package com.lxk.controller;

import com.lxk.pojo.Delivery;
import com.lxk.pojo.GoodsCom;
import com.lxk.pojo.Orders;
import com.lxk.service.OrdersService;
import com.lxk.vo.OrdersDetailModel;
import com.lxk.vo.PageModel;
import com.lxk.vo.PointOrdersModel;
import com.lxk.vo.param.OrdersParam;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.OrdersInfoModel;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author JohnSoybean
 * @create 2020/3/30 16:45
 * @description
 */
@Controller
public class OrdersController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrdersService os;

    /**
     * 订单列表展示
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/orders")
    public String toOrdersPage(Model model, Integer pageIndex, Integer num, Integer totalPages, OrdersParam ordersParam) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = os.getTotalPages(num, ordersParam);
        }
        PageModel<OrdersInfoModel> ordersInfo = os.selOrdersPage(pageIndex, num, totalPages, ordersParam);
        model.addAttribute("data", ordersInfo);
        return "orders";
    }


    /**
     * 待发货订单列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/express")
    public String toOrdersPage(HttpServletRequest req, Model model,
                               Integer pageIndex, Integer num, Integer totalPages,
                               Integer sort, Integer order) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = os.getUnsendTotalPages(num);
        }
        PageModel<OrdersInfoModel> ordersInfo = os.selUnsendOrdersPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", ordersInfo);
        return "express";
    }


    /**
     * 获取收货地址信息
     * #改为从订单中直接获取
     * @param ordersId
     * @return
     */
    @GetMapping("/orders/delivery")
    @ResponseBody
    public Result<Delivery> getDelivery(Integer ordersId) {
        Orders orders = os.selOrdersById(ordersId);
        Delivery delivery = new Delivery();
        delivery.setDeliveryAddr(orders.getDeliveryAddr());
        delivery.setDeliveryCity(orders.getDeliveryCity());
        delivery.setDeliveryId(orders.getDeliveryId());
        delivery.setDeliveryName(orders.getDeliveryName());
        delivery.setDeliveryPhone(orders.getDeliveryPhone());
        delivery.setWxUserId(orders.getWxUserId());
        return Result.success(delivery);
    }

    /**
     * 设置发货信息
     * @param ordersId
     * @param expressNo
     * @return
     */
    @PostMapping("/orders/send")
    @ResponseBody
    public Result<Integer> setExpress(Integer ordersId, String expressNo) {
        os.setExpress(ordersId, expressNo);
        return Result.success(1);
    }


    /**
     * 获取订单详情信息
     * @param ordersId
     * @return
     */
    @GetMapping("/orders/query")
    @ResponseBody
    public Result<OrdersDetailModel> setExpress(Integer ordersId) {
        OrdersDetailModel ordersDetailModel = os.queryOrders(ordersId);
        return Result.success(ordersDetailModel);
    }


    /**
     * 积分好礼中奖订单列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/pointOrders")
    public String toPointOrdersPage(HttpServletRequest req, Model model,
                               Integer pageIndex, Integer num, Integer totalPages,
                               Integer sort, Integer order,
                               Integer ordersStatus ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = os.getPointOrderTotalPages(num);
        }
        PageModel<PointOrdersModel> ordersInfo = os.selPointOrdersPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", ordersInfo);
        return "pointOrders";
    }

    /**
     * 获取收货地址信息
     * @param pointOrdersId
     * @return
     */
    @GetMapping("/pointOrders/delivery")
    @ResponseBody
    public Result<Delivery> getpointOrdersDelivery(Integer pointOrdersId) {
        Delivery delivery = os.selDeliveryFromPointOrders(pointOrdersId);
        return Result.success(delivery);
    }

    /**
     * 设置积分中奖发货信息
     * @param pointOrdersId
     * @param expressNo
     * @return
     */
    @PostMapping("/pointOrders/send")
    @ResponseBody
    public Result<Integer> setPointOrdersExpress(Integer pointOrdersId, String expressNo) {
        os.setPointOrdersExpress(pointOrdersId, expressNo);
        return Result.success(1);
    }


    /**
     * 获取订单评价
     * @param ordersId
     * @return
     */
    @GetMapping("/orders/comment")
    @ResponseBody
    public Result<GoodsCom> getGoodsComment(Integer ordersId) {
        GoodsCom goodsCom = os.getGoodsComByOrdersId(ordersId);
        if (goodsCom==null) {
            return Result.error(CodeMsg.ORDER_NO_COMMENT);
        }else{
            return Result.success(goodsCom);
        }
    }

    /**
     * 删除订单评价
     * @param goodsComId
     * @return
     */
    @GetMapping("/orders/comment/delete")
    @ResponseBody
    public Result<Integer> deleteGoodsComment(Integer goodsComId) {
        log.info(goodsComId+"");
        os.delGoodsCom(goodsComId);
        return Result.success(1);
    }

}
